﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HomeWork.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ImgController : ControllerBase
    {
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file">图片文件</param>
        /// <returns>符合条件的数据</returns>
        [HttpPost]
        public IActionResult Img(IFormFile file)
        {
            var f = file.ContentType;
            List<string> ls = new List<string>
            {
                "image/png",
                 "image/jpeg",
                  "image/jpg",
                   "image/webp",

            };
            if (!ls.Contains(f))
            {
                return Ok("不对");
            }
            var max = 1024 * 1024 * 2;
            if (file.Length > max)
            {
                return Ok("大小不对");
            }
            string path = Directory.GetCurrentDirectory() + @"/wwwroot/PRO/" + file.FileName;
            using (FileStream fs = new FileStream(path,FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            return Ok("https://localhost:7130/PRO/" + file.FileName);
        }
    }
}
